import { _decorator, animation, Color, Component, Label, Node, ProgressBar, Sprite, SpriteFrame, tween, UITransform, v3, Vec3 } from 'cc';
import { Task } from './Task';
import { _wheel } from '../../Main';
const { ccclass, property } = _decorator;

@ccclass('Customer')
export class Customer extends Component {
    
    @property(Sprite)
    pic: Sprite = null!

    @property(Node)
    content: Node = null!

    @property(Node)
    zone: Node = null!

    @property([Color])
    colors: Color[] = []
    
    @property([SpriteFrame])
    pics: SpriteFrame[] = []

    @property(Node)
    complain: Node = null!

    @property(Label)
    words: Label = null!

    complaining: boolean = false

    tasks: Task[] = []

    sat: boolean = false

    picNum: number = 0

    init(tasks: Task[]) {
        this.sat = false
        this.tasks = tasks
        let length: number = this.tasks.length
        for (let i = 0; i < length; i++) {
            let task: Task = this.tasks[i]
            let tn: Node = task.node
            tn.parent = this.zone
            let x: number = 0
            if (length == 1) {
                x = 0
                task.rescale(1.4)
            } else if (length == 2) {
                x = -90 + i * 180
                task.rescale(1.2)
            } else if (length == 3) {
                x = -130 + i * 130
                task.rescale(1)
            }
            tn.position = v3(x, 0, 0)
        }
        this.hideAll()
    }

    hideAll() {
        this.hideContent(false)
        this.hideComplain()
    }

    getContentCenter(): Vec3 {
        let cpos: Vec3 = this.content.getPosition()
        let zpos: Vec3 = this.zone.getPosition()
        let scale: number = this.node.scale.x
        return v3(cpos.x + zpos.x, cpos.y + zpos.y, cpos.z + zpos.z).multiply(v3(scale, scale, scale))
    }

    getPicCenter(): Vec3 {
        let scale: number = this.node.scale.x
        return this.pic.node.getPosition().clone().multiply(v3(scale, scale, scale))
    }

    getFinishedSize(): number {
        let size: number = 0
        for (let i = 0; i < this.tasks.length; i++) {
            let task: Task = this.tasks[i]
            if (task.finished) {
                size++
            }
        }
        return size
    }

    showContent(anime: boolean = false, then?: () => void) {
        if (!anime) {
            this.content.scale = v3(1, 1, 1)
            if (then) {
                then()
            }
        } else {
            tween(this.content)
                .call(() => {
                    this.content.scale = v3(0, 0, 0)
                })
                .to(.2, { scale: v3(1.1, 1.1, 1.1) })
                .to(.1, { scale: v3(1, 1, 1) })
                .call(() => {
                    if (then) {
                        then()
                    }
                })
                .start()
        }
    }

    hideContent(anime: boolean = false, then?: () => void) {
        if (!anime) {
            this.content.scale = v3(0, 0, 0)
            if (then) {
                then()
            }
        } else {
            tween(this.content)
                .to(.1, { scale: v3(1.1, 1.1, 1.1) })
                .to(.2, { scale: v3(0, 0, 0) })
                .call(() => {
                    if (then) {
                        then()
                    }
                })
                .start()
        }
    }

    changePic(index: number = 0) {
        if (this.picNum == index) {
            return
        }
        this.pic.spriteFrame = this.pics[index]
        this.picNum = index
    }

    doComplain(words: string) {
        if (this.complaining) {
            return
        }
        this.words.string = words
        let r: number = _wheel.getRandomInt(0, 2)
        let x: number = r == 0 ? -1 : 1
        tween(this.complain)
            .call(() => {
                this.complaining = true
                this.words.node.scale = v3(x, 1, 1)
            })
            .to(0.5, { scale: v3(x, 1, 1) })
            .delay(3)
            .to(0.5, { scale: v3(0, 0, 0) })
            .delay(1)
            .call(() => {
                this.complaining = false
            })
            .start()
    }

    hideComplain() {
        this.complain.scale = v3(0, 0, 0)
    }
}


